package com.panda.mess.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.panda.mess.entity.Message;
import com.panda.mess.entity.MessageReply;
import com.panda.mess.entity.MessageVo;
import com.panda.mess.service.MessageService;
import com.panda.sys.entity.User;
import com.panda.utils.CommonUtils;
import com.panda.utils.Pager;
import com.panda.utils.contantUtil;
import com.panda.ws.WebSocketServer;

@Controller
public class MessageController {
	@Resource
	private MessageService messageService;
	
	@RequestMapping("/mess/chat")
	public String chat(Model model,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		Map<Long, Session> sessionMap = WebSocketServer.sessionMap;
		List<User> friends=messageService.findAllFriend(user.getId());
		Iterator<User> iterator = friends.iterator();
		while (iterator.hasNext()) {
			User next = iterator.next();
			if(!sessionMap.containsKey(next.getId())){
				iterator.remove();
			}
		}
		model.addAttribute("friends", friends);
		return "mess/index";
	}
	
	@RequestMapping("mess/list")
	public String list() {
		return "mess/list";
	}
	
 
	@RequestMapping("/mess/getlist")
	@ResponseBody
	public String getlist(Model model,Long offSet,Long pageSize,String name,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		Pager<MessageVo> Groups=messageService.findListLimit(offSet, pageSize,user.getId(),name);
		return JSON.toJSONString(Groups);
	}
	
 
	@RequestMapping("/mess/setIsRead")
	@ResponseBody
	public String setIsRead(Model model,Message message){
		messageService.update(message);
		List<MessageVo> list=messageService.findAllMess(message.getId());
		return CommonUtils.msgCallback(true,"保存成功",null, JSON.toJSONString(list));
	}
	
 
	@RequestMapping("/mess/save")
	@ResponseBody
	public String save(Model model,Message message,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		message.setFrom(user.getId());
		message.setIsRead(1);
		messageService.save(message);
		return CommonUtils.msgCallback(true,"保存成功",null,null);
	}
 
	@RequestMapping("/mess/saverep")
	@ResponseBody
	public String saverep(Model model,MessageReply messageReply,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		messageReply.setFormId(user.getId());
		messageService.saverep(messageReply);
		Message message=new Message();
		message.setId(messageReply.getMid());
		message.setIsRead(1);
		messageService.update(message);
		return CommonUtils.msgCallback(true,"保存成功",null, null);
	}
	
}
